import plugin from '../../../lib/plugins/plugin.js'
import Config from '../models/Config.js'
import Render from '../models/Render.js'
import _ from 'lodash'
import fs from 'fs'
import { pluginRoot } from '../models/Path.js'
import { config } from 'process'
const { author_id } = JSON.parse(fs.readFileSync(`${pluginRoot}/package.json`, 'utf-8'))

export class Setting extends plugin {
    constructor() {
        super({
            name: '小月设置',
            dsc: '小月设置',
            event: 'message',
            priority: 9999,
            rule: [
                {
                    reg: '^#?小月设置class ?(开启|关闭)$',
                    fnc: 'settingClass',
                },
                {
                    reg: '^#?小月设置class_url ?(.+)',
                    fnc: 'settingClassUrl',
                },
                {
                    reg: '^#?小月设置plus ?(开启|关闭)$',
                    fnc: 'settingPlus',
                },
                {
                    reg: '^#?小月设置plus_url ?(.+)',
                    fnc: 'settingPlusUrl',
                },
                {
                    reg: '^#?小月设置emoji ?(开启|关闭)$',
                    fnc: 'settingEmoji',
                },
                {
                    reg: '^#?小月设置emoji_url ?(.+)',
                    fnc: 'settingEmojiUrl',
                },
                {
                    reg: '^#?小月设置ai ?(开启|关闭)$',
                    fnc: 'settingAI',
                },
                {
                    reg: '^#?小月设置poke ?(开启|关闭)$',
                    fnc: 'settingPoke',
                },
                {
                    reg: '^#?小月设置random ?(\\d+(\\.\\d+)?)$',
                    fnc: 'settingRndomImgReply',
                },
                {
                    reg: '^#?小月设置random_video ?(\\d+(\\.\\d+)?)$',
                    fnc: 'settingRndomVideoReply',
                },
                {
                    reg: '^#?小月设置random_record ?(\\d+(\\.\\d+)?)$',
                    fnc: 'settingRndomRecordReply',
                },
                {
                    reg: '^#?小月设置source ?(\\d)$',
                    fnc: 'settingSource',
                },
                {
                    reg: '^#?小月设置$',
                    fnc: 'setting',
                },
                {
                    reg: '^#?小月(设置)?(说话|安静)$',
                    fnc: 'settingSwitch',
                },
            ]
        })
    }

    async settingClass(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        const type = e.msg.includes("开启")
        if (type !== Config.getConfig().class) {
            Config.getConfig().class = type
        }
        return await this.#render(e)
    }
    async settingClassUrl(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        let [, classUrl] = e.msg.match(this.rule[1].reg) || []
        if (!classUrl) return await e.reply("请检查输入")
        classUrl = classUrl.trim()
        Config.getConfig().class_url = classUrl
        return await this.#render(e)
    }
    async settingPlus(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        const type = e.msg.includes("开启")
        if (type !== Config.getConfig().plus) {
            Config.getConfig().plus = type
        }
        return await this.#render(e)
    }
    async settingPlusUrl(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        let [, plusUrl] = e.msg.match(this.rule[3].reg) || []
        if (!plusUrl) return await e.reply("请检查输入")
        plusUrl = plusUrl.trim()
        Config.getConfig().plus_url = plusUrl
        return await this.#render(e)
    }
    async settingEmoji(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        const type = e.msg.includes("开启")
        if (type !== Config.getConfig().emoji) {
            Config.getConfig().emoji = type
        }
        return await this.#render(e)
    }
    async settingEmojiUrl(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        let [, emojiUrl] = e.msg.match(this.rule[5].reg) || []
        if (!emojiUrl) return await e.reply("请检查输入")
        emojiUrl = emojiUrl.trim()
        Config.getConfig().emoji_url = emojiUrl
        return await this.#render(e)
    }
    async settingAI(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        const type = e.msg.includes("开启")
        if (type !== Config.getConfig().ai) {
            Config.getConfig().ai = type
        }
        return await this.#render(e)
    }
    async settingPoke(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        const type = e.msg.includes("开启")
        if (type !== Config.getConfig().poke) {
            Config.getConfig().poke = type
        }
        return await this.#render(e)
    }
    async settingRndomImgReply(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        let [, num] = e.msg.match(this.rule[8].reg) || []
        if (!num) return await e.reply("请检查输入")
        num = Number(num)
        if (num === NaN) return await e.reply("输入有误")
        if (num > 1 || num < 0) return await e.reply("输入区间为 0-1")
        Config.getConfig().random = num
        return await this.#render(e)
    }
    async settingRndomVideoReply(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        let [, num] = e.msg.match(this.rule[9].reg) || []
        if (!num) return await e.reply("请检查输入")
        num = Number(num)
        if (num === NaN) return await e.reply("输入有误")
        if (num > 1 || num < 0) return await e.reply("输入区间为 0-1")
        Config.getConfig().random_video = num
        return await this.#render(e)
    }
    async settingRndomRecordReply(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        let [, num] = e.msg.match(this.rule[10].reg) || []
        if (!num) return await e.reply("请检查输入")
        num = Number(num)
        if (num === NaN) return await e.reply("输入有误")
        if (num > 1 || num < 0) return await e.reply("输入区间为 0-1")
        Config.getConfig().random_record = num
        return await this.#render(e)
    }
    async settingSource(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        let [, num] = e.msg.match(this.rule[11].reg) || []
        if (!num) return await e.reply("请检查输入")
        num = Math.floor(Number(num))
        if (num === NaN) return await e.reply("输入有误")
        if (num > 2 || num < 0) return await e.reply("输入区间为 0-2")
        Config.getConfig().source = num
        return await this.#render(e)
    }
    async settingSwitch(e) {
        if (!this.e.isMaster && String(this.e.user_id) !== author_id) return true
        const config = Config.getConfig()
        const type = e.msg.includes("说话")
        if (!Array.isArray(config.switch)) config.switch = []
        const index = config.switch.indexOf(e.group_id)
        if (!type && index < 0) {
            config.switch.push(e.group_id)
        }
        if (type && index >= 0) {
            config.switch.splice(index, 1)
        }
        type ? await e.reply("我可以说话了") : await e.reply("我安静了")
        return true
    }
    async setting(e) {
        return await this.#render(e)
    }
    async #render(e) {
        const config = Config.getConfig()

        let cfg = _.cloneDeep(config)
        let schema = {
            Group: {
                title: 'xiaoyue设置',
                cfg: {
                    class: {
                        title: '逆天图设置',
                        key: 'class',
                        def: true,
                        desc: '逆天图开关',
                        fileName: 'xysz'
                    },
                    class_url: {
                        title: '逆天图url',
                        key: 'class_url',
                        def: true,
                        desc: '设置逆天图url',
                        fileName: 'xysz'
                    },
                    plus: {
                        title: 'plus',
                        key: 'plus',
                        def: true,
                        desc: 'plus开关',
                        fileName: 'xysz'
                    },
                    plus_url: {
                        title: 'plus_url',
                        key: 'plus_url',
                        def: true,
                        desc: '设置plus url',
                        fileName: 'xysz'
                    },
                    emoji: {
                        title: '表情包设置',
                        key: 'emoji',
                        def: true,
                        desc: '表情包开关',
                        fileName: 'xysz'
                    },
                    emoji_url: {
                        title: 'emoji_url',
                        key: 'emoji_url',
                        def: true,
                        desc: '设置表情包url',
                        fileName: 'xysz'
                    },
                    random: {
                        title: '图片回复概率',
                        key: 'random',
                        def: true,
                        desc: '回复为图片概率 0-1',
                        fileName: 'xysz'
                    },
                    random_video: {
                        title: '视频回复概率',
                        key: 'random_video',
                        def: true,
                        desc: '回复为视频概率 0-1',
                        fileName: 'xysz'
                    },
                    random_record: {
                        title: '音频回复概率',
                        key: 'random_record',
                        def: true,
                        desc: '回复为音频概率 0-1',
                        fileName: 'xysz'
                    },
                    ai: {
                        title: '关键词回复',
                        key: 'ai',
                        def: true,
                        desc: '关键词回复开关',
                        fileName: 'xysz'
                    },
                    poke: {
                        title: '戳一戳',
                        key: 'poke',
                        def: true,
                        desc: '设置开启戳一戳随机回复',
                        fileName: 'xysz'
                    },
                    source: {
                        title: 'source',
                        key: 'source',
                        def: true,
                        desc: '插件安装源 0默认 1gitee 2github',
                        fileName: 'xysz'
                    }
                }
            }
        }
        let imgPlus = false

        // 渲染图像
        return await Render.render('admin/index', {
            schema,
            cfg,
            imgPlus
        }, { e, scale: 1.4 })
    }
}

